﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpDiary.Core.Common;
using SharpDiary.Core.Attributes;
using SharpDiary.Core.ORMapping;
using SharpDiary.Core.Security;

namespace SharpDiary.Diary
{
    [GuiModule("48d65655-85b9-4503-a0a6-3adea7fe6b71", "Diary private password editor", "Ray Shaw(demogodyou@gmail.com)", true)]
    public partial class frmDiaryPrivatePassword : GuiModuleUIBase
    {
        #region Fields & Properties

        private DiaryInfo _diary;
        private DiaryInfoListItemControl _sender;

        public DiaryInfo Diary
        {
            get { return _diary; }
            set { _diary = value; }
        }

        public string Title
        {
            get { return this.Text; }
            set { this.Text = value; }
        }

        /// <summary>
        /// 获取或设置密码。
        /// </summary>
        public string Password
        {
            get { return txtPassword.Text; }
            set { txtPassword.Text = value; }
        }

        /// <summary>
        /// 获取或设置确认密码。
        /// </summary>
        public string ConfirmPassword
        {
            get { return txtConfirmPassword.Text; }
            set { txtConfirmPassword.Text = value; }
        }


        #endregion

        #region Constructors

        public frmDiaryPrivatePassword()
        {
            InitializeComponent();
        }

        #endregion

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            Close();
        }

        private bool CheckIfInputOk()
        {
            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                ShowMessage("请输入密码。");
                txtPassword.Focus();
                txtPassword.SelectAll();

                return false;
            }

            if (txtPassword.Text != txtConfirmPassword.Text)
            {
                ShowMessage("确认密码不正确，请重新输入。");
                txtConfirmPassword.Focus();
                txtConfirmPassword.SelectAll();

                return false;
            }

            return true;
        }

        public override void SetParameter(object parameter, object sender)
        {
            base.SetParameter(parameter, sender);

            _diary = parameter as DiaryInfo;
            _sender = sender as DiaryInfoListItemControl;
        }

        /// <summary>
        /// 更新日记独立密码。
        /// </summary>
        private bool UpdateDiaryPrivatePassword()
        {
            if (_diary == null) { return false; }

            _diary.PrivatePasswordMD5 = MD5Helper.GenerateToMD5HexString(txtPassword.Text);

            return DiaryInfo.Update<DiaryInfo>(_diary);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!CheckIfInputOk()) { return; }

            if (UpdateDiaryPrivatePassword())
            {
                if (_sender != null) { _sender.Diary = Diary; }

                ShowMessage("操作成功。");
                DelayClose();
            }
            else
            {
                ShowMessage("操作失败。");
                DelayClose();
            }

            this.DialogResult = DialogResult.OK;
            Close();
        }

        #region Methods

        public override string GetSettingsKeyName()
        {
            return this.Name;
        }

        #endregion
    }
}
